package com.cnsuning.jobtool.jobtools.deletesql;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class DeleteSqlGenerator {
    private static final Logger LOGGER = LoggerFactory.getLogger(DeleteSqlGenerator.class);
    //======15-22=======
    //25
    public static final String SO_CHG_STATUS_INF = "delete from (select * from omsdadm.SO_CHG_STATUS_INF where CREATed_TIME <'2019-03-01' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
//    public static final String SO_CHG_STATUS_INF = "delete from omsdadm.SO_CHG_STATUS_INF where ROW_ID in(select ROW_ID from omsdadm.SO_CHG_STATUS_INF where CREATed_TIME <'2019-03-01' FETCH FIRST 1000000 ROWS ONLY WITH UR);";

    //20
    public static final String SO_SALES_ORDER_MAIN_INF = "delete from (select * from omsdadm.SO_SALES_ORDER_MAIN_INF where CREATed_TIME <'2019-03-01' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
//    public static final String SO_SALES_ORDER_MAIN_INF = "delete from omsdadm.SO_SALES_ORDER_MAIN_INF where ROW_ID in(select ROW_ID from omsdadm.SO_SALES_ORDER_MAIN_INF where CREATed_TIME <'2019-03-01' FETCH FIRST 1000000 ROWS ONLY WITH UR);";

    //24
    public static final String SO_DATA_SYN_INFO1 = "delete from (select * from omsdadm.SO_DATA_SYN_INFO where CREAT_TIME <'2019-06-18' and SYN_FALG='2' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
//    public static final String SO_DATA_SYN_INFO1 = "delete from omsdadm.SO_DATA_SYN_INFO where ROW_ID in(select ROW_ID from omsdadm.SO_DATA_SYN_INFO where CREAT_TIME <'2019-06-18' and SYN_FALG='2' FETCH FIRST 1000000 ROWS ONLY WITH UR);";

    //8
    public static final String SO_DATA_SYN_DELI_INFO = "delete from (select * from omsdadm.SO_DATA_SYN_DELI_INFO where CREAT_TIME <'2019-03-01' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
//    public static final String SO_DATA_SYN_DELI_INFO = "delete from omsdadm.SO_DATA_SYN_DELI_INFO where ROW_ID in(select ROW_ID from omsdadm.SO_DATA_SYN_DELI_INFO where CREAT_TIME <'2019-03-01' FETCH FIRST 1000000 ROWS ONLY WITH UR);";

    //======23-30=======
    //55
    public static final String STATUS_SYN_INFO = "delete from (select * from omsdadm.STATUS_SYN_INFO where MODIFY_TIME <'2019-06-18' and SYN_FALG='2' and BUSINESS_SCENE != 'OMSD_SAC_001' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
//    public static final String STATUS_SYN_INFO = "delete from omsdadm.STATUS_SYN_INFO where ROW_ID in(select ROW_ID from omsdadm.STATUS_SYN_INFO where MODIFY_TIME <'2019-06-18' and SYN_FALG='2' and BUSINESS_SCENE != 'OMSD_SAC_001' FETCH FIRST 1000000 ROWS ONLY WITH UR);";

    //120
    public static final String SO_DATA_SYN_INFO2 = "delete from (select * from omsdadm.SO_DATA_SYN_INFO where CREAT_TIME <'2019-06-18' and SYN_FALG='2' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
//    public static final String SO_DATA_SYN_INFO2 = "delete from omsdadm.SO_DATA_SYN_INFO where ROW_ID in(select ROW_ID from omsdadm.SO_DATA_SYN_INFO where CREAT_TIME <'2019-06-18' and SYN_FALG='2' FETCH FIRST 1000000 ROWS ONLY WITH UR);";

    //349
    public static final String SO_BUDS_ST_CHGLOG = "delete from (select * from omsdadm.SO_BUDS_ST_CHGLOG where CREATed_TIME <'2019-08-18' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
//    public static final String SO_BUDS_ST_CHGLOG = "delete from omsdadm.SO_BUDS_ST_CHGLOG where ROW_ID in(select ROW_ID from omsdadm.SO_BUDS_ST_CHGLOG where CREATed_TIME <'2019-08-18' FETCH FIRST 1000000 ROWS ONLY WITH UR);";
    public static void main(String[] args) throws IOException {
        DeleteSqlGenerator deleteSqlGenerator = new DeleteSqlGenerator();
        deleteSqlGenerator.generator();
    }


    public void generator() throws IOException {
        String filePath = "src\\main\\java\\com\\cnsuning\\jobtool\\jobtools\\deletesql";
        LOGGER.info("====准备开始====");
        Map<String,Integer> map15to22 = new HashMap<>();
        map15to22.put(SO_CHG_STATUS_INF,25);
        map15to22.put(SO_SALES_ORDER_MAIN_INF,20);
        map15to22.put(SO_DATA_SYN_INFO1,24);
        map15to22.put(SO_DATA_SYN_DELI_INFO,8);

        Map<String,Integer> map23to30 = new HashMap<>();
        map23to30.put(STATUS_SYN_INFO,55);
        map23to30.put(SO_DATA_SYN_INFO2,120);
        map23to30.put(SO_BUDS_ST_CHGLOG,310);
        LOGGER.info("=====15-22库sql开始=====");
        File file1 = new File(filePath+File.separator+"15-22.sql");
        if (file1.exists()) file1.delete();
        for (Map.Entry<String,Integer> entry : map15to22.entrySet())
        {
            String sql = entry.getKey();
            int num = entry.getValue();
            for (int i = 0; i < num; i++) {
                FileUtils.writeStringToFile(file1,sql+"\n","utf-8",true);
            }
        }

        LOGGER.info("=====15-22库sql结束=====");

        LOGGER.info("=====23-30库sql开始=====");
        File file2 = new File(filePath+File.separator+"23-30.sql");
        if (file2.exists()) file2.delete();
        for (Map.Entry<String,Integer> entry : map23to30.entrySet())
        {
            String sql = entry.getKey();
            int num = entry.getValue();
            for (int i = 0; i < num; i++) {
                FileUtils.writeStringToFile(file2,sql+"\n","utf-8",true);
            }
        }

        LOGGER.info("=====23-30库sql结束=====");

        LOGGER.info("====结束====");
    }
}
